package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slmxz.tms.common.constant.SystemConstants;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.document.mapper.BusinessMapper;
import com.slmxz.tms.document.model.entity.Business;
import com.slmxz.tms.document.model.vo.BusinessVO;
import com.slmxz.tms.document.service.BusinessService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 业务类型服务实现类
 * @author sunyao
 * @Date 2024-11-09
 */
@Service
@RequiredArgsConstructor
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    /**
    * 获取业务类型分页列表
    *
    * @return {@link IPage< BusinessVO >} 业务类型分页列表
    */
    @Override
    public IPage<BusinessVO> selectBusinessPage(IPage<BusinessVO> page,BusinessVO business) {
        return page.setRecords(this.baseMapper.selectBusinessPage(page,business));
    }
    
    /**
     * 获取业务类型表单数据
     *
     * @param id 业务类型ID
     * @return
     */
    @Override
    public BusinessVO selectBusinessDetail(Long id) {
        Business entity = this.getById(id);
        Asserts.fail(entity == null,BizMessageEnum.BIZ0000001);

        return BeanUtil.copyProperties(entity,BusinessVO.class);
    }
    
    /**
     * 新增业务类型
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBusiness(BusinessVO business) {
        //查询当前业务代码是否存在
        QueryWrapper<Business> businessQuery = new QueryWrapper<>();
        businessQuery.lambda().eq(Business::getName,business.getName())
                .eq(Business::getIsDeleted, SystemConstants.UN_DELETED);
        long count = this.count(businessQuery);
        Asserts.fail(count > 0,BizMessageEnum.BIZ0000011);

        Business entity = BeanUtil.copyProperties(business,Business.class);
        boolean result = this.save(entity);
        Asserts.fail(!result,BizMessageEnum.BIZ0000002);
        return true;
    }
    
    /**
     * 更新业务类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBusiness(BusinessVO business) {
        Asserts.fail(business.getId() == null, BizMessageEnum.PARAM_IS_NULL);

        Business entity = this.getById(business.getId());

        Asserts.fail(entity == null, BizMessageEnum.BIZ0000001);
        entity.setId(business.getId());
        entity.setName(business.getName());
        entity.setRemark(business.getRemark());
        return this.updateById(entity);
    }
    
    /**
     * 删除业务类型
     *
     * @param idStr 业务类型ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBusinesss(String idStr) {
        Asserts.fail(StrUtil.isBlank(idStr), BizMessageEnum.PARAM_IS_NULL);

        // 物理删除
        List<Long> ids = Arrays.stream(idStr.split(","))
                .map(Long::parseLong)
                .toList();

        //删除业务数据
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(BusinessVO business) {
        //激活或者注销业务数据
        Asserts.fail(CollectionUtil.isEmpty(business.getIds()) || business.getIsDeleted() == null, BizMessageEnum.PARAM_IS_NULL);

        List<Business> businessList = new ArrayList<>(business.getIds().size());
        business.getIds().forEach(id -> {
            Business businessParam = new Business();
            businessParam.setId(id);
            businessParam.setIsDeleted(business.getIsDeleted());
            businessList.add(businessParam);
        });
        this.updateBatchById(businessList);
        return true;
    }
}
